<#assign shiro=JspTaglibs["http://shiro.apache.org/tags"]>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>
        编辑用户
    </title>

    <style type="text/css">
        body {
            padding: 10px 0;
        }

        div.ztree {
            border: 1px solid #617775;
            background: #f0f6e4;
            width: 300px;
            height: 300px;
            overflow-y: scroll;
            overflow-x: auto;
        }

        #result_id table {
            width: 100%;
        }

        #result_id table tr td:hover {
            background-color: #FF9999;
            cursor: pointer
        }
    </style>

    <link rel="stylesheet" href="/common/js/zTree/css/zTreeStyle/zTreeStyle.css" type="text/css">
    <link rel="stylesheet" href="/common/js/bootstrap-3.0.0/css/bootstrap-united.min.css" type="text/css">
    <link rel="stylesheet" href="/common/js/jquery-plugin/validate/style.css" type="text/css">
</head>
<body>
<#if user??>
<div class="container">
    <form id="form" method="post" class="form-horizontal">
        <fieldset>
            <legend>
                编辑用户
            </legend>
            <input type="hidden" id="id" name="id" value="${user.id}">

            <div class="form-group">
                <label class="col-md-2 control-label" for="userName">用户名:</label>

                <div class="col-md-4">
                    <input type="text" id="userName" class="form-control" name="userName" required="required"
                           placeholder="字母，数字"
                           <#if user??>disabled="disabled" </#if>
                           value="${(user.username)!''}">
                </div>
            </div>

            <div class="form-group">
                <label class="col-md-2 control-label" for="cnName">昵称:</label>

                <div class="col-md-4">
                    <input type="text" id="cnName" class="form-control" name="cnName" value="${(user.cnName)!''}"
                           required="required">
                </div>
            </div>

            <div class="form-group">
                <label class="col-md-2 control-label" for="trueName">真名:</label>

                <div class="col-md-4">
                    <input type="text" id="trueName" class="form-control" name="trueName" value="${(user.trueName)!''}"
                           required="required" maxlength="12">
                </div>
            </div>

            <div class="form-group">
                <label class="col-md-2 control-label" for="identity">身份证:</label>

                <div class="col-md-4">
                    <input type="text" id="identity" class="form-control" name="identity" value="${(user.identity)!''}"
                           required="required" maxlength="18">
                </div>
            </div>

            <div class="form-group">
                <label class="col-md-2 control-label" for="email">邮箱:</label>

                <div class="col-md-4">
                    <input type="text" id="email" name="email" class="form-control" value="${(user.email)!''}"
                           placeholder="请输入邮箱">
                </div>
            </div>

            <div class="form-group">
                <label class="col-md-2 control-label" for="login_name">用户类型:</label>

                <div class="col-md-4">
                    <select id="user_type" name="user_type" class="form-control">
                        <option value=""></option>
                        <#if userTypeList??>
                            <#list userTypeList as userType>
                                <option value="${userType.getCode()?string}" <#if userType.getCode() == user.getUserType()>selected</#if>>${userType.getName()}</option>
                            </#list>
                        </#if>
                    </select>
                </div>
            </div>

            <div class="form-group">
                <label class="col-md-2 control-label" for="login_name">省市区:</label>

                <div id="district" class="col-md-4">
                    <select name="province" required="required"></select>
                    <select name="city" required="required"></select>
                    <select name="area"></select>
                </div>
            </div>

        <#if is_biz??>
            <div class="form-group">
                <label class="col-md-2 control-label" for="roleDiv">商家服务:</label>

                <div class="col-md-4">
                    <#--<div id="workTypeDiv" class="ztree">-->

                    <#--</div>-->

                    <select id="biz_work_type" name="biz_work_type" class="form-control">
                        <option value="1" <#if user.getBizWorkType()?? && 1 == user.getBizWorkType()>selected</#if>>策划师</option>
                        <option value="5" <#if user.getBizWorkType()?? && 5 == user.getBizWorkType()>selected</#if>>主持人</option>
                        <option value="6" <#if user.getBizWorkType()?? && 6 == user.getBizWorkType()>selected</#if>>摄影师</option>
                        <option value="7" <#if user.getBizWorkType()?? && 7 == user.getBizWorkType()>selected</#if>>化妆师</option>
                        <option value="27" <#if user.getBizWorkType()?? && 27 == user.getBizWorkType()>selected</#if>>摄像师</option>
                        <#if workTypeList??>
                            <#list workTypeList as workType>
                                <option value="${workType.getId()?string}" <#if user.getBizWorkType()?? && workType.getId() == user.getBizWorkType()>selected</#if>>${workType.getName()}</option>
                            </#list>
                        </#if>
                    </select>
                </div>
            </div>


            <#if user.getUserType() == 3 || user.getUserType() == 4>
                <div class="form-group">
                    <label class="col-md-2 control-label" for="roleDiv">所属代理商:</label>

                    <div class="col-md-4">
                        <select id="biz_agent_id" name="biz_agent_id" class="form-control">
                            <#if agentList??>
                                <#list agentList as agent>
                                    <option value="${agent.getId()?string}" <#if user.getBizAgentId()?? && agent.getId() == user.getBizAgentId()>selected</#if>>${agent.getCnName()}</option>
                                </#list>
                            </#if>
                        </select>
                    </div>
                </div>
            </#if>

            <#if byChannelKeywords??  && (byChannelKeywords?size>0) >
                <div class="form-group">
                    <label class="col-md-2 control-label" for="qq">搜索关键字:</label>

                    <div class="col-md-10">
                            <#list byChannelKeywords as keyword>
                                <label><input type="checkbox" name="tag_ids" id="tag_ids_zs" value="${keyword.getId()}"
                                            <#assign tagList = user.tagIds!''>
                                              <#if tagList??>
                                                <#if tagList?index_of(",") != -1>
                                                    <#if tagList?contains(keyword.getId()+",") || tagList?contains(","+keyword.getId())>
                                                        checked="checked"
                                                    </#if>
                                                <#else >
                                                    <#if tagList?string == keyword.getId()?string>
                                                        checked="checked"
                                                    </#if>
                                                </#if>
                                              </#if>
                                />${keyword.name}&nbsp;&nbsp;</label>
                            </#list>
                    </div>
                </div>
            </#if>

            <div class="form-group">
                <label class="col-md-2 control-label" for="qq">服务平均价格(元):</label>

                <div class="col-md-4">
                    <input type="text" id="biz_price" name="biz_price" class="form-control" value="${(user.biz_price)!''}" >
                </div>
            </div>

        </#if>

            <div class="form-group">
                <label class="col-md-2 control-label" for="email">权重:</label>

                <div class="col-md-4">
                    <input type="text" id="is_hot" name="is_hot" class="form-control" value="${(user.is_hot)!''}"
                           placeholder="请输入权重,值越大排在前面。">
                </div>
            </div>

            <div class="form-group">
                <label class="col-md-2 control-label" for="roleDiv">权限:</label>

                <div class="col-md-4">
                    <div id="roleDiv" class="ztree">

                    </div>
                </div>
            </div>

            <div class="form-group">
                <label class="col-md-2 control-label" for="descn">描述:</label>

                <div class="col-md-4">
                    <textarea id="descn" name="descn" class="form-control">${(user.descn)!''}</textarea>
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-offset-2 col-md-4">
                    <button class="btn btn-primary" type="submit">保存</button>
                    <button class="btn btn-danger" onclick="window.close();">取消</button>
                </div>
            </div>
        </fieldset>
    </form>
</div>
<#else>
<div class="container">
    <hr>
    用户不存在
</div>
</#if>
<script type="text/javascript" src="/common/js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="/common/js/jquery.citys.js"></script>
<script type="text/javascript" src="/common/js/zTree/js/jquery.ztree.core-3.5.min.js"></script>
<script type="text/javascript" src="/common/js/zTree/js/jquery.ztree.excheck-3.5.min.js"></script>
<script type="text/javascript" src="/common/js/jquery-plugin/validate/jquery.validate.1.11.1.min.js"></script>
<script type="text/javascript">
    $('#district').citys({code:'${area}'});
    <#--$(".province").val(${province});-->
    <#--$(".city").val(${city});-->
    <#--$(".area").val(${area});-->
</script>

<script type="text/javascript">
    $.validator.setDefaults({
        submitHandler: function () {
            saveModify();
        }
    });

    jQuery.validator.addMethod("regex", //addMethod第1个参数:方法名称
            function (value, element, params) {     //addMethod第2个参数:验证方法，参数（被验证元素的值，被验证元素，参数）
                var exp = new RegExp(params);     //实例化正则对象，参数为传入的正则表达式
                return exp.test(value);                  //测试是否匹配
            }, "格式错误");    //addMethod第3个参数:默认错误信息

    $(function () {
        $("#form").validate({
            rules: {
                userName: {
                    required: true,
                    rangelength: [5, 12],
                    regex: "^[_a-zA-Z0-9]*$",
                    remote: {
                        url: "/user/isUser.json",
                        type: "get",
                        data: {
                            userName: function () {
                                return encodeURI($("#userName").val().trim());
                            }
                        }
                    }
                }
            },
            messages: {
                userName: {
                    required: "请输入用户名",
                    rangelength: "长度必须介于{0} 和 {1} 之间的字符串",
                    regex: "用户名只能是数字、字母与下划线",
                    remote: "用户名已存在"
                }
            },
            highlight: function (element) {

                $(element).next("label.valid").remove();
                $(element).closest('.form-group').removeClass('success').addClass('error');
            },
            unhighlight: function (element) {
                if ($(element).next("label.valid").length == 0) {
                    $(element).after("<label class='valid'/>");
                }
                $(element).closest('.form-group').removeClass('error').addClass('success');
            }
        });
    });

    $(function () {
        $.ajax({
            type: "get",
            url: "/user/getRoleTree.json",
            data: {id:${user.id}},
            success: function (data) {
                var setting = {
                    check: {
                        enable: true,
                        chkboxType: { "Y": "", "N": "" }
                    },
                    view: {
                        selectedMulti: false
                    },
                    data: {
                        simpleData: {
                            enable: true,
                            idKey: "id",
                            pIdKey: "parentId"
                        },
                        key: {
                            name: "roleName"
                        }
                    },
                    callback: {
                        onCheck: function (event, treeId, treeNode) {
                            var treeObj = $.fn.zTree.getZTreeObj("roleDiv");
                            var nodes = treeObj.getNodesByFilter(function () {
                                return true;
                            }, false, treeNode);

                            for (var k in nodes) {
                                if (treeNode.checked) {
                                    treeObj.checkNode(nodes[k], false);
                                }

                                treeObj.setChkDisabled(nodes[k], treeNode.checked);
                            }
                        }
                    }
                };

                var obj = $.fn.zTree.init($("#roleDiv"), setting, data.list);
                var nodes = obj.getCheckedNodes(true);
                disableNodes(obj, nodes);
            }
        });
    });

    //禁用子节点
    function disableNodes(treeObj, nodes) {
        for (var k in nodes) {
            var treeNode = nodes[k];
            var subNodes = treeObj.getNodesByFilter(function () {
                return true;
            }, false, treeNode);

            for (var m in subNodes) {
                treeObj.setChkDisabled(subNodes[m], true);
            }
        }
    };

    //保存修改
    function saveModify() {
        var param = $("#form").serialize();

        //role
        var treeObj = $.fn.zTree.getZTreeObj("roleDiv");
//    var nodes = treeObj.getChangeCheckedNodes();
        var nodes = treeObj.getNodesByFilter(function (node) {
            return node.checked != node.checkedOld;
        }, false);

        if (nodes.length > 0) {
            var list = "[";
            for (var i = 0; i < nodes.length; i++) {
                if (i > 0) {
                    list += ",";
                }
                list += "{\"id\":" + nodes[i].id + ",\"checked\":" + nodes[i].checked + "}"
            }
            list += "]";
            param += "&roleList=" + list;
        }

        var url = "/user/updateUser.json";
        $.post(url, param, function (data) {
            if (data) {
                alert("操作成功");
                if (window.opener && window.opener.search) {
                    window.opener.search();
                }
                window.close();
            } else {
                alert("操作失败");
            }
        });
    };
</script>
</body>
</html>